using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DesktopWidgetApp.Core.Data;
using DesktopWidgetApp.Shared;

namespace DesktopWidgetApp.Core.Services
{
    /// <summary>
    /// 任务服务
    /// </summary>
    public class TaskService
    {
        private readonly ITaskRepository _taskRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="taskRepository">任务仓储</param>
        public TaskService(ITaskRepository taskRepository)
        {
            _taskRepository = taskRepository;
        }

        /// <summary>
        /// 获取所有任务
        /// </summary>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetAllTasksAsync()
        {
            return await _taskRepository.GetAllAsync();
        }

        /// <summary>
        /// 获取指定ID的任务
        /// </summary>
        /// <param name="id">任务ID</param>
        /// <returns>任务</returns>
        public async Task<TaskItem> GetTaskByIdAsync(Guid id)
        {
            return await _taskRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 创建新任务
        /// </summary>
        /// <param name="task">任务</param>
        /// <returns>创建的任务</returns>
        public async Task<TaskItem> CreateTaskAsync(TaskItem task)
        {
            if (task == null)
                throw new ArgumentNullException(nameof(task));

            task.Id = Guid.NewGuid();
            task.CreatedTime = DateTime.Now;
            task.Status = TaskStatus.Todo;

            return await _taskRepository.AddAsync(task);
        }

        /// <summary>
        /// 更新任务
        /// </summary>
        /// <param name="task">任务</param>
        /// <returns>更新后的任务</returns>
        public async Task<TaskItem> UpdateTaskAsync(TaskItem task)
        {
            if (task == null)
                throw new ArgumentNullException(nameof(task));

            // 如果任务状态变为已完成，设置完成时间
            if (task.Status == TaskStatus.Completed && !task.CompletedTime.HasValue)
            {
                task.CompletedTime = DateTime.Now;
            }

            return await _taskRepository.UpdateAsync(task);
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="id">任务ID</param>
        /// <returns>是否删除成功</returns>
        public async Task<bool> DeleteTaskAsync(Guid id)
        {
            return await _taskRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 根据状态获取任务
        /// </summary>
        /// <param name="status">任务状态</param>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetTasksByStatusAsync(TaskStatus status)
        {
            return await _taskRepository.GetByStatusAsync(status);
        }

        /// <summary>
        /// 根据优先级获取任务
        /// </summary>
        /// <param name="priority">优先级</param>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetTasksByPriorityAsync(Priority priority)
        {
            return await _taskRepository.GetByPriorityAsync(priority);
        }

        /// <summary>
        /// 获取截止日期前的任务
        /// </summary>
        /// <param name="dueDate">截止日期</param>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetTasksByDueDateAsync(DateTime dueDate)
        {
            return await _taskRepository.GetByDueDateAsync(dueDate);
        }

        /// <summary>
        /// 根据标签获取任务
        /// </summary>
        /// <param name="tag">标签</param>
        /// <returns>任务列表</returns>
        public async Task<IEnumerable<TaskItem>> GetTasksByTagAsync(string tag)
        {
            return await _taskRepository.GetByTagAsync(tag);
        }
    }
}